<?php require '../../src/facebook.php';
$params = array(
  
  'redirect_uri' => 'http://phsarkhmer.biz/posts/adsadd'
);
// Create our Application instance (replace this with your appId and secret).
 $facebook = new Facebook(array(
    'appId' => '624388847632649',
    'secret' => '832da640540ec5d0c33e2128111d8fb0',
        ));

// Get User ID
 $user = $facebook->getUser();
//echo $user;

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
     $_SESSION['facebook']='facebook';
     $_SESSION['userid']=$facebook->getUser();
     $_SESSION['username']= $user_profile['name'];
    
     $_SESSION['gender']=$user_profile['gender'];
    //$facebook->destroySession();
} else {
  //  $statusUrl = $facebook->getLoginStatusUrl();

    $loginUrl = $facebook->getLoginUrl($params);
}

$userdata = $this->session->read('Auth.User');
$username = $this->session->read('Auth.User.username');
$role = $this->session->read('Auth.User.role');
if ($user) {
    $role = $user;
}
?>
<?php if (!$role) { ?><h3 class="list-group-item active header-side">User Login</h3>
    <div class="user-login">

        <?php echo $this->Session->flash('auth'); ?>
        <?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'login'))); ?>



        <?php
        echo $this->Form->input('role', array('type' => 'hidden', 'value' => 'register'));
        echo $this->Form->input('username', array('class' => 'form-control', 'placeholder' => 'Username'));
        echo $this->Form->input('password', array('class' => 'form-control', 'placeholder' => 'Password'));
        ?>

        <?php echo $this->Form->end(__('Login'), array('class' => 'btn')); ?>

        <?php echo $this->Html->link(__('Forgot Password?'), array('controller' => 'posts', 'action' => 'forgotpws')) . '<br>'; ?>
    <?php echo $this->Html->link(__('Create New Account?'), array('controller' => 'posts', 'action' => 'register')); ?>
    <!--	<button type="submit" class="btn facebook-login"><i class="facebook-icon"></i>Login Facebook</button>-->

        <div>
           
            <a style="font-size: 11px;" href="<?php echo $loginUrl; ?>"><img src="<?php echo $this->webroot ?>img/facebook-login.png" /></a>
        </div>
    </div>
<?php
} else {
    echo '<h3 class="list-group-item active header-side">User Login</h3>';
    echo '<div class="user-login">';
    if ($user) {
        ?> <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><?php
        echo $user_profile['name'];
        ?>
             
 <ul>
      <li>
          <?php
          
              echo $this->Html->link('Post Free Ads', array('controller' => 'posts', 'action' => 'adsadd'));
          ?>
      </li>
        <li>
          <?php
              echo $this->Html->link('Manage Ads', array('controller' => 'posts', 'action' => 'manageads'));
          ?>
        </li>
        <li>
          <?php
              echo $this->Html->link('Profile', array('controller' => 'posts', 'action' => 'edituser', $userdata));
          ?>
        </li>
        <li><?php echo $this->Html->link(__('Logout'), array('controller' => 'posts', 'action' => 'facelogout','class'=>'btn')); ?></li>
    </ul><?php
    }
    
    if ($username) {
        echo 'Welcome <b>' . $username . '!</b>';
        echo '<span class="btn logout">' . $this->Html->link(__('Logout'), array('controller' => 'posts', 'action' => 'logout')) . '</span><br>';
    }

    //echo $this->Html->link(__('Forgot Password?'), array('controller' => 'posts', 'action' => 'forgotpws')).'<br>';
    //echo $this->Html->link(__('Create New Account?'), array('controller' => 'posts', 'action' => 'register'));
    //echo '<button type="submit" class="btn facebook-login"><i class="facebook-icon"></i>Login Facebook</button>';

    echo '</div>';
}
?>